package com.ls.task.controller.api;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.ls.fw.commons.log.exception.ExceptionsHelper;
import com.ls.fw.data.bean.ResultStatus;
import com.ls.fw.data.bean.ResultTemplate;
import com.ls.fw.mvc.springmvc.base.BaseCRUDController;
import com.ls.fw.web.core.servlet.utils.HttpUtils;
import com.ls.task.biz.QuertzBiz;
import com.ls.task.biz.SysTaskTriggerBiz;
import com.ls.task.dal.form.SysTaskForm;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
/** 
 * 
 * @author ls
 */
@Controller
@RequestMapping(value = "/task/api")
@Api(value = "系统任务API")
public class TaskApiController extends BaseCRUDController
{
  
	public static final String MODEL = "api_task";
	public static final String MODEL_NAME = "系统任务";

	@Autowired
	private QuertzBiz  quertzBiz;
	@Autowired
	private SysTaskTriggerBiz sysTaskTriggerBiz;
  
    @RequestMapping("") 
    public ModelAndView index(HttpServletRequest req) {
        ModelAndView mavAndView = new ModelAndView("/sys/task/api");
        mavAndView.addAllObjects(HttpUtils.getAllPara(req,false));
        return mavAndView;
    }  
    
    
    /**
     * 调用器状态
     * @param webRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/status") 
    @ApiOperation(value="status", notes = "调用器状态")
    public ModelAndView status(WebRequest webRequest) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					obj.setData(quertzBiz.status());
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  

    /**
     * 调用任务
     * @param webRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/fire/job") 
    public ModelAndView fire(WebRequest webRequest) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.fire();
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    
    /**
     * 调用任务
     * @param webRequest
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/fire/job/{id}") 
    public ModelAndView fire(WebRequest webRequest, @PathVariable("id") final Long id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.fire(id);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  

    /**
     * 暂停任务
     * @param webRequest
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/pause/job/{id}") 
    public ModelAndView pauseJob(WebRequest webRequest, @PathVariable("id") final Long id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.pauseJobs(id);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    

    /**
     * 暂停任务
     * @param webRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/pause/job") 
    public ModelAndView pauseJob(WebRequest webRequest) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.pauseAllJobs();
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    

    /**
     * 暂停触发器
     * @param webRequest
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/pause/trigger/{id}") 
    public ModelAndView pauseTrigger(WebRequest webRequest, @PathVariable("id") final Long id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.pauseTriggers(id);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    

    /**
     * 暂停触发器
     * @param webRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/pause/trigger") 
    public ModelAndView pauseTrigger(WebRequest webRequest) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.pauseAllTriggers();
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    
    /**
     * 恢复任务
     * @param webRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/resume/job") 
    public ModelAndView resumeJob(WebRequest webRequest) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.resumeAllJobs();;
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    

    /**
     * 恢复任务
     * @param webRequest
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/resume/job/{id}") 
    public ModelAndView resumeJob(WebRequest webRequest, @PathVariable("id") final Long id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.resumeJobs(id);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    

    /**
     * 恢复触发器
     * @param webRequest
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/resume/trigger/{id}") 
    public ModelAndView resumeTrigger(WebRequest webRequest, @PathVariable("id") final Long id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.resumeTriggers(id);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    

    /**
     * 恢复触发器
     * @param webRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/resume/trigger") 
    public ModelAndView resumeTrigger(WebRequest webRequest) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.resumeAllTriggers();
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    
    

    /**
     * 关闭
     * @param webRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/shutdown") 
    public ModelAndView shutdown(WebRequest webRequest) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.shutdown();
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  

    /**
     * 启动
     * @param webRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/start") 
    public ModelAndView start(WebRequest webRequest) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.start();
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    
    

    /**
     * 加载所有任务
     * @param webRequest
     * @return
     * @throws Exception
     */
    @RequestMapping("/load") 
    public ModelAndView load(WebRequest webRequest) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					sysTaskTriggerBiz.load();
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    


    /**
     * 加载触发器
     * @param webRequest
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/load/trigger/{id}") 
    public ModelAndView loadTrigger(WebRequest webRequest, @PathVariable("id") final Long id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.scheduleJobs(id);;
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    

    /**
     * 卸载触发器
     * @param webRequest
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/unload/trigger/{id}") 
    public ModelAndView unloadTrigger(WebRequest webRequest, @PathVariable("id") final Long id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.unscheduleJobs(id);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    

    
    /**
     * 卸载任务
     * @param webRequest
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/unload/job/{id}") 
    public ModelAndView unloadJob(WebRequest webRequest, @PathVariable("id") final Long id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.deleteJobs(id);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
    

    /**
     * 加载任务
     * @param webRequest
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/load/job/{id}") 
    public ModelAndView loadJob(WebRequest webRequest, @PathVariable("id") final Long id) throws Exception {
    	String method = Thread.currentThread() .getStackTrace()[1].getMethodName();
    	String mapping = this.methodRequestMapping.get(method);
		return this.doSave(null, webRequest, this.viewPrefix+mapping
             ,  new SaveWorkHandler<SysTaskForm>() {

			@Override
			public ResultTemplate doWork(SysTaskForm form, WebRequest webRequest)
					throws Exception {
				ResultTemplate obj = ResultTemplate.newInstance();
				try {
					quertzBiz.addJobs(id);
					obj.setStatus(ResultStatus.OK);
				} catch (Exception e) {
					logger.error(e.getMessage(), e);
					obj.setMsg(ExceptionsHelper.getRootCause(e).getMessage());
				}
				return obj;
			}
		});
    }  
}
